import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F

# 读取文件内容
file_path = "/home/cc/AAA_python_test/tmp&anchor/tmp_anchor_20250222_2113/driver_100_30frame/05251517_0433.MP4/00660.lines.txt"
with open(file_path, 'r') as file:
    data = file.read()

# 定义tensor
tensor = torch.tensor

# 提取tensor数据
tensor_data = eval(data.strip())

# 获取组数
num_groups = min(4, len(tensor_data[0]))

# 分割成三个长度为33的分布
X = torch.stack([tensor_data[0][i][:33].clone().detach() for i in range(num_groups)])
Y = torch.stack([tensor_data[0][i][33:66].clone().detach() for i in range(num_groups)])
T = torch.stack([tensor_data[0][i][66:99].clone().detach() for i in range(num_groups)])

# 应用softmax函数
X_softmax = F.softmax(X, dim=1)
Y_softmax = F.softmax(Y, dim=1)
T_softmax = F.softmax(T, dim=1)

# 绘制柱状图
fig, axs = plt.subplots(num_groups,3,  figsize=(10 * num_groups, 15))

if num_groups == 1:
    for i in range(num_groups):
        axs[0].bar(range(33), X_softmax[i].numpy())
        axs[0].set_title(f'X Distribution - Group {i + 1}')

        axs[1].bar(range(33), Y_softmax[i].numpy())
        axs[1].set_title(f'Y Distribution - Group {i + 1}')

        axs[2].bar(range(33), T_softmax[i].numpy())
        axs[2].set_title(f'T Distribution - Group {i + 1}')
else :
    for i in range(num_groups):
        axs[i, 0].bar(range(33), X_softmax[i].numpy())
        axs[i, 0].set_title(f'X Distribution - Group {i + 1}')

        axs[i, 1].bar(range(33), Y_softmax[i].numpy())
        axs[i, 1].set_title(f'Y Distribution - Group {i + 1}')

        axs[i, 2].bar(range(33), T_softmax[i].numpy())
        axs[i, 2].set_title(f'T Distribution - Group {i + 1}')
plt.tight_layout()
plt.show()
